package com.ccp.dev.workflow.bpmutil;

import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.FlowElementsContainer;
import org.flowable.bpmn.model.SubProcess;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * ClassDescribe:bpmn工具类
 * @author :jiayueqiao
 * Date: 2019-06-04
 * Since:1
 */
public class BPMN20Util {

    /**
     * 根据类型获取流程元素
     * @param process 流程
     * @param deepIntoSub 深入到内部子流程
     * @param flowTypes 类型
     * @return 流程元素列表
     */
    public static List<FlowElement> getFlowElementByType(FlowElementsContainer process, boolean deepIntoSub, Class... flowTypes) {

        List<FlowElement> flowElements = new ArrayList();
        //获取流程的所有元素
        Collection<FlowElement> flowElementCollection = process.getFlowElements();
        Iterator iterator = flowElementCollection.iterator();
        while(iterator.hasNext()) {
            FlowElement flowElement = (FlowElement)iterator.next();
            Class[] classes = flowTypes;

            int len$ = flowTypes.length;
            for(int i$ = 0; i$ < len$; i$++) {
                Class<? extends FlowElement> flowType = classes[i$];
                if (flowType.isInstance(flowElement)) {
                    flowElements.add(flowElement);
                    break;
                }
            }

            //内部子流程
            if (deepIntoSub && flowElement instanceof SubProcess) {
                flowElements.addAll(getFlowElementByType((SubProcess)flowElement, true, flowTypes));
            }
        }
        return flowElements;

    }
}
